package com.hjm.common.utils.pay;

import com.alibaba.fastjson.JSON;
import com.hjm.common.exception.BusinessException;
import com.hjm.common.utils.DesUtil;
import com.hjm.common.utils.Utils;
import com.hjm.framework.config.CustomConfig;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.*;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.util.HashMap;

/**
 * @ClassName CkUtils
 * @Deacription 出款，扣款相关
 * @Author Administrator
 * @Version 1.0
 **/
public class CkUtils {

    /**
     * 提现扣款
     */
    public static final String zz_url="http://10.71.11.13:15800/qb-pro/agent/addAgentMoney.do";
    //public static final String zz_url="http://192.168.1.40:8080/qb-pro/agent/addAgentMoney.do";


    private static Logger logger = LogManager.getLogger("resultPack");


    public static String sendData(String ip,Integer port,byte[] bytes){
        logger.info("请求地址:" + ip + "-" + port);
        Socket socket = null;
        OutputStream out = null;
        InputStream inputStream = null;
        BufferedReader bReader = null;
        String msg = null;
        try {
            socket = new Socket(ip, port);
            out = socket.getOutputStream();
            inputStream = socket.getInputStream();
            ByteBuffer header = ByteBuffer.allocate(1024);
            header.clear();
            header.put(bytes);
            header.flip();
            out.write(header.array());
            out.flush();
            bReader = new BufferedReader(new InputStreamReader(inputStream)) ;
            msg = bReader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bReader.close();
                out.close();
                socket.close();

            } catch (IOException e) {
            }
        }
        return msg;
    }

    public static String getType(String money,String type,String agent_num,String agent_name){
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("agent_num", agent_num); //代理商编号
        map.put("agent_name", agent_name); //代理商名称
        map.put("money", money); //充值金额
        map.put("underlay_name", "钱宝北京运用中心"); //<暂时 名称写死>
        map.put("underlay_num", "000000"); //总账户编号<000000写死>
        map.put("type", 6); //请求类型 1 充值附带扣款 2、垫资充值
//		map.put("man", "admin"); //操作人 （谁充值的）
        map.put("man", type); //操作人 （谁充值的）   admin 激活的
        String md5Hex = Utils.MD5(JSON.toJSONString(map));
        JSONObject obj = new JSONObject();
        obj.put("body", map);
        obj.put("md5", md5Hex.toLowerCase());
        return obj.toString();
    }


    public static boolean sendKou(String money,String type,String agent_num,String agent_name) throws Exception{
        boolean flag = false;
        String setData = getType(money,type,agent_num,agent_name);
        logger.info("请求数据:" + setData);
        String encrypt = DesUtil.encrypt(setData.getBytes(), DesUtil.KEY);
        String s = sendData(CustomConfig.getVirtualAccountUrl(),CustomConfig.getVirtualAccountPort(),encrypt.getBytes());
        logger.info("返回结果:" + s);
        if (!StringUtils.isEmpty(s)){
            JSONObject jsonObject = JSONObject.fromObject(s);
            String code = String.valueOf(jsonObject.get("code"));
            logger.info("返回code:" + code);
            if(!"1006".equals(code)) {
                throw new BusinessException(String.valueOf(jsonObject.get("message")));
            }
        } else {
            throw new BusinessException("接口返回为空:" + s);
        }
        return true;
    }





}
